﻿Imports System
Imports System.CodeDom
Imports System.Collections
Imports System.ComponentModel.Design.Serialization
Imports System.Workflow.ComponentModel

Namespace System.Workflow.ComponentModel.Serialization
    Public Class ActivityTypeCodeDomSerializer
        Inherits TypeCodeDomSerializer
        ' Methods
        Public Overrides Function Deserialize(ByVal manager As IDesignerSerializationManager, ByVal declaration As CodeTypeDeclaration) As Object
            Return MyBase.Deserialize(manager, declaration)
        End Function

        Protected Overrides Function GetInitializeMethod(ByVal manager As IDesignerSerializationManager, ByVal typeDecl As CodeTypeDeclaration, ByVal value As Object) As CodeMemberMethod
            If (manager Is Nothing) Then
                Throw New ArgumentNullException("manager")
            End If
            If (typeDecl Is Nothing) Then
                Throw New ArgumentNullException("typeDecl")
            End If
            If (value Is Nothing) Then
                Throw New ArgumentNullException("value")
            End If
            Dim method As CodeMemberMethod = [TryCast](typeDecl.UserData.Item(ActivityTypeCodeDomSerializer._initMethodKey),CodeMemberMethod)
            If (method Is Nothing) Then
                method = New CodeMemberMethod { _
                    .Name = "InitializeComponent", _
                    .Attributes = MemberAttributes.Private _
                }
                typeDecl.UserData.Item(ActivityTypeCodeDomSerializer._initMethodKey) = method
                Dim constructor As New CodeConstructor { _
                    .Attributes = MemberAttributes.Public _
                }
                constructor.Statements.Add(New CodeMethodInvokeExpression(New CodeThisReferenceExpression, "InitializeComponent", New CodeExpression(0  - 1) {}))
                typeDecl.Members.Add(constructor)
            End If
            Return method
        End Function

        Protected Overrides Function GetInitializeMethods(ByVal manager As IDesignerSerializationManager, ByVal typeDecl As CodeTypeDeclaration) As CodeMemberMethod()
            If (manager Is Nothing) Then
                Throw New ArgumentNullException("manager")
            End If
            If (typeDecl Is Nothing) Then
                Throw New ArgumentNullException("typeDecl")
            End If
            Dim member As CodeTypeMember
            For Each member In typeDecl.Members
                Dim method As CodeMemberMethod = [TryCast](member,CodeMemberMethod)
                If (((Not method Is Nothing) AndAlso method.Name.Equals("InitializeComponent")) AndAlso (method.Parameters.Count = 0)) Then
                    Return New CodeMemberMethod() { method }
                End If
            Next
            Return New CodeMemberMethod(0  - 1) {}
        End Function

        Public Overrides Function Serialize(ByVal manager As IDesignerSerializationManager, ByVal root As Object, ByVal members As ICollection) As CodeTypeDeclaration
            If (manager Is Nothing) Then
                Throw New ArgumentNullException("manager")
            End If
            If (root Is Nothing) Then
                Throw New ArgumentNullException("root")
            End If
            Dim activity As Activity = [TryCast](root,Activity)
            If (activity Is Nothing) Then
                Throw New ArgumentException(SR.GetString("Error_UnexpectedArgumentType", New Object() { GetType(Activity).FullName }), "root")
            End If
            Dim declaration As CodeTypeDeclaration = MyBase.Serialize(manager, root, members)
            Dim method As CodeMemberMethod = [TryCast](declaration.UserData.Item(ActivityTypeCodeDomSerializer._initMethodKey),CodeMemberMethod)
            If ((Not method Is Nothing) AndAlso TypeOf activity Is CompositeActivity) Then
                Dim array As CodeStatement() = New CodeStatement(method.Statements.Count  - 1) {}
                method.Statements.CopyTo(array, 0)
                method.Statements.Clear
                Dim statement As New CodeAssignStatement { _
                    .Left = New CodePropertyReferenceExpression(New CodeThisReferenceExpression, "CanModifyActivities"), _
                    .Right = New CodePrimitiveExpression(True) _
                }
                method.Statements.Add(statement)
                Dim statement2 As CodeStatement
                For Each statement2 In array
                    method.Statements.Add(statement2)
                Next
                Dim statement3 As New CodeAssignStatement { _
                    .Left = New CodePropertyReferenceExpression(New CodeThisReferenceExpression, "CanModifyActivities"), _
                    .Right = New CodePrimitiveExpression(False) _
                }
                method.Statements.Add(statement3)
            End If
            Dim member As CodeTypeMember
            For Each member In declaration.Members
                Dim field As CodeMemberField = [TryCast](member,CodeMemberField)
                If (Not field Is Nothing) Then
                    Dim obj2 As Object
                    For Each obj2 In members
                        If Not TypeOf obj2 Is Activity Then
                            Throw New ArgumentException(SR.GetString("Error_UnexpectedArgumentType", New Object() { GetType(Activity).FullName }), "members")
                        End If
                        Dim activity2 As Activity = [TryCast](obj2,Activity)
                        If (((field.Name = manager.GetName(activity2)) AndAlso (CInt(activity2.GetValue(ActivityMarkupSerializer.StartLineProperty)) <> -1)) AndAlso (Not activity.GetValue(ActivityCodeDomSerializer.MarkupFileNameProperty) Is Nothing)) Then
                            field.LinePragma = New CodeLinePragma(CStr(activity.GetValue(ActivityCodeDomSerializer.MarkupFileNameProperty)), Math.Max(CInt(activity2.GetValue(ActivityMarkupSerializer.StartLineProperty)), 1))
                        End If
                    Next
                    [Continue] For
                End If
            Next
            Return declaration
        End Function


        ' Fields
        Private Shared _initMethodKey As Object = New Object
        Private Const _initMethodName As String = "InitializeComponent"
    End Class
End Namespace

